package com.yjhb.android.sbz.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.tabs.TabLayout;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.ShopInfo;
import com.yjhb.android.sbz.entity.UserFollow;
import com.yjhb.android.sbz.fragment.FollowFragment;
import com.yjhb.android.sbz.fragment.PublishTaskListFragment;
import com.yjhb.android.sbz.mvp.userinfo.UserInfoPresenter;
import com.yjhb.android.sbz.mvp.userinfo.UserInfoView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.adapter.FragmentAdapter;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.tool.ZxNetTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/11/19 9:22
 */
public class UserInfoActivity extends BaseActivity<UserInfoPresenter> implements UserInfoView {
    private final String TAG = "UserInfoActivity";
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.title_tv)
    TextView title_tv;
    @BindView(R.id.my_head_iv)
    CircleImageView my_head_iv;
    @BindView(R.id.my_name_tv)
    TextView my_name_tv;
    @BindView(R.id.my_id_tv)
    TextView my_id_tv;
    @BindView(R.id.follow_tv)
    TextView follow_tv;
    @BindView(R.id.progressbar1)
    ProgressBar progressbar1;
    @BindView(R.id.fraction_tv)
    TextView fractionTv;
    @BindView(R.id.title_right_tv)
    TextView title_right_tv;
    @BindView(R.id.earnestMoney_tv)
    TextView earnestMoneyTv;
    @BindView(R.id.sendTaskCount_tv)
    TextView sendTaskCountTv;
    @BindView(R.id.totalTaskCount_tv)
    TextView totalTaskCountTv;
    @BindView(R.id.acceptMissionCount_tv)
    TextView acceptMissionCountTv;
    @BindView(R.id.reportedCount_tv)
    TextView reportedCountTv;
    @BindView(R.id.favorableRate_tv)
    TextView favorableRateTv;
    @BindView(R.id.fansCount_tv)
    TextView fansCountTv;
    @BindView(R.id.data_layout)
    LinearLayout dataLayout;
    @BindView(R.id.expansion_tv)
    TextView expansionTv;
    @BindView(R.id.splitLine)
    View splitLine;
    @BindView(R.id.vip_flag)
    ImageView vip_flag;


    private int userId;
    private UserFollow userFollow;
    private ShopInfo shopInfo;
    List<BaseFragment> fragments = new ArrayList<>();
    PublishTaskListFragment publishTaskListFragment;
    FollowFragment fragment2;
    FollowFragment fragment3;

    FragmentAdapter fragmentAdapter;


    @Override
    protected UserInfoPresenter initPresenter() {
        return new UserInfoPresenter(this);
    }

    @Override
    protected boolean isCheckLogin() {
        return false;
    }

    @Override
    protected boolean isImmerse() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_myinfo;
    }

    @Override
    protected void initUI() {
        userId = getIntent().getIntExtra("userId", -1);
        if (userId == -1) {
            ZxToast.showINFOBottomToast("用户信息错误");
            return;
        }
        initViewPagerAndTab();
        update();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {

    }

    @Override
    public void refresh() {
        presenter.getUserBehavior(userId);
        if (App.userInfo != null && ("" + userId).equals(App.userInfo.getId())) {
            userFollow = new UserFollow();
            userFollow.setId(App.userInfo.getId());
            userFollow.setVipLevel(App.userInfo.getVipLevel());
            userFollow.setNickName(App.userInfo.getNickName());
            userFollow.setAvatar(App.userInfo.getAvatar());
            userFollow.setScore(App.userInfo.getScore());
            update();
            return;
        }
        presenter.getUserInfo(userId);

    }

    @Override
    public void onEventMsg(EventMsg msg) {
        if (msg.msg == EventMsg.USER_UPDATE_OK) {
            update();
        }
    }

    @OnClick(R.id.follow_tv)
    void follow_tv() {
        if (userFollow == null) {
            Tool.checkLogin(this);
            return;
        }
        presenter.follow(userId, userFollow.isFollow() ? 2 : 1);
    }

    @OnClick(R.id.expansion_tv)
    void expansion_tv() {
        if (expansionTv.getText().toString().equals("展开")) {
            splitLine.setVisibility(View.VISIBLE);
            dataLayout.setVisibility(View.VISIBLE);
            expansionTv.setText("收起");
        } else {
            splitLine.setVisibility(View.GONE);
            dataLayout.setVisibility(View.GONE);
            expansionTv.setText("展开");
        }
    }

    private void update() {
        if (userFollow != null) {
            Glide.with(this).load(userFollow.getAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(my_head_iv);
            my_name_tv.setText(userFollow.getNickName());
            my_id_tv.setText("ID:" + userFollow.getId());
            int vipLevel = userFollow.getVipLevel();
            vip_flag.setVisibility(View.VISIBLE);
            switch (vipLevel) {
                case 100:
                    vip_flag.setImageResource(R.mipmap.exp);
                    break;
                case 1:
                    vip_flag.setImageResource(R.mipmap.month);
                    break;
                case 2:
                    vip_flag.setImageResource(R.mipmap.season);
                    break;
                case 3:
                    vip_flag.setImageResource(R.mipmap.year);
                    break;
                default:
                    vip_flag.setVisibility(View.GONE);
            }

            if (userFollow.getId().equals(App.userInfo.getId())) {
                follow_tv.setVisibility(View.INVISIBLE);
                title_right_tv.setVisibility(View.GONE);
                title_tv.setText("我的店铺");
            } else {
                title_right_tv.setVisibility(View.GONE);
                follow_tv.setVisibility(View.VISIBLE);
                title_tv.setText(userFollow.getNickName() + "的店铺");
                if (userFollow.isFollow()) {
                    follow_tv.setText("取消关注");
                } else {
                    follow_tv.setText("+关注");
                }
            }
            progressbar1.setMax(Tool.getMaxScore(userFollow.getScore()));
            progressbar1.setProgress(userFollow.getScore());
            fractionTv.setText(userFollow.getScore() + "/" + (userFollow.getScore() < 0 ? 0 : Tool.getMaxScore(userFollow.getScore())));

        }
        if (shopInfo != null) {
            earnestMoneyTv.setText(String.valueOf(Tool.getPrettyNumber(shopInfo.getCreditMoney())));
            sendTaskCountTv.setText(String.valueOf(shopInfo.getPublishTaskNum()));
            totalTaskCountTv.setText(String.valueOf(shopInfo.getMyTaskOverCount()));
            acceptMissionCountTv.setText(String.valueOf(shopInfo.getOverTaskNum()));
            reportedCountTv.setText(String.valueOf(shopInfo.getReported()));
            favorableRateTv.setText(shopInfo.getReview() + "%");
            fansCountTv.setText(new StringBuilder("粉丝数:").append(shopInfo.getFansNum()));
        }
    }

    private void initViewPagerAndTab() {
        fragments.clear();
        fragments.add(publishTaskListFragment = new PublishTaskListFragment());
        publishTaskListFragment.setUserId(userId);
        boolean b = false;
        if (App.userInfo != null)
            b = App.userInfo.getId().equals("" + userId);
        if (b) {
            fragments.add(fragment2 = new FollowFragment());
            fragment2.setType(1);
            fragments.add(fragment3 = new FollowFragment());
            fragment3.setType(2);
        }
        viewpager.setAdapter(fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments));
        tabLayout.setupWithViewPager(viewpager);
        tabLayout.getTabAt(0).setText("    任务    ");
        if (b) {
            tabLayout.getTabAt(1).setText("    关注    ");
            tabLayout.getTabAt(2).setText("    粉丝    ");
            tabLayout.setVisibility(View.VISIBLE);
        } else {
            tabLayout.setVisibility(View.GONE);
        }


    }

    public void onBack(View view) {
        finish();
    }

    public void settingInfo(View view) {
        startActivity(new Intent(this, SettingInfoActivity.class));
    }

    @Override
    public void onWait() {
        loadingDialog.setTipsText("加载中...");
        loadingDialog.show();
    }

    @Override
    public void onSuccess(UserFollow data) {
        loadingDialog.cancel();
        userFollow = data;
        update();
    }

    @Override
    public void onFollow() {
        loadingDialog.cancel();
        refresh();
    }

    @Override
    public void onShopInfo(ShopInfo shopInfo) {
        this.shopInfo = shopInfo;
        update();
    }


    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        if (!ZxNetTool.isNetworkAvailable(this)) {
            ZxToast.showINFOBottomToast("网络连接失败");
            return;
        }
        if (code != -1) {
            ZxToast.showINFOBottomToast(msg);
        }
    }

}
